/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package admingui;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
 *
 * @author jost0908
 */
public class AdminXML {

    private ArrayList<Item> itemList;
    private Item itemC;
    
    public void AdminXML(){
        
    }

     // list><item><name>Milk</name><id>1</id><price>100</price><quantity>900</quantity></item>
        public ArrayList createItemFromXML(String xml){

            itemList = new ArrayList<Item>();
        try {

            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            Document doc = db.parse(is);

            String name, id, price, quantity;

            NodeList item = doc.getElementsByTagName("item");
            Element elements = doc.getDocumentElement();
            int len = item.getLength();

            for(int i = 0; i < len; i++){
                
                Element line = (Element) elements.getElementsByTagName("name").item(i);
                name = getStringFromElement(line);

                line = (Element) elements.getElementsByTagName("id").item(i);
                id = getStringFromElement(line);

                line = (Element) elements.getElementsByTagName("price").item(i);
                price = getStringFromElement(line);

                line = (Element) elements.getElementsByTagName("quantity").item(i);
                quantity = getStringFromElement(line);
                
                itemC = new Item(id, name, price, quantity);
                itemList.add(itemC);
                
            }
        } catch (Exception ex) {
            System.out.println("createItemFromXML exception: " + ex);
        }
            return itemList;
    }

     // output examples
    // remove some: <remove><id>3</id><quantity>23</quantity></remove>
    // remove all:  <remove><id>3</id></remove>
   public String removeToXML(String id, String quantity){
     
       String removeItemXML = "<remove><id>"+id+"</id></remove>";
       if(quantity != null || quantity.length() != 0){
          String removeQuantityXML = "<remove><id>"+id+"</id><quantity>"+quantity+"</quantity></remove>";
          return removeQuantityXML;
       }else
           return removeItemXML;
       
   }
    // input example: <?xml version="1.0" encoding="UTF-8" standalone="no"?><currentMonth>2</currentMonth>
   public String getMonthFromXML(String monthXML){
 
        String month = "";
        try {
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(monthXML));
            Document doc = db.parse(is);
            Element root = doc.getDocumentElement();
            month = root.getFirstChild().getTextContent();
            
        } catch (Exception ex) {
            System.out.println("getMonthFromXML EX: " + ex);
          }
        
        return month;

      }
   
   public String addItemToXML(String id, String name, String price, String quantity){
       String addItemXML = "<add><id>"+id+"</id><name>"+name+"</name><price>"+price+"</price><quantity>"+quantity+"</quantity></add>";
       
       return addItemXML;
   }

   public String getStringFromElement(Element e) {
        Node child = e.getFirstChild();

        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
        return "";
    }
}
